package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Answer;
import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.mapper.AnswerMapper;
import cn.wolfcode.trip.base.mapper.QuestionMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IQuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private AnswerMapper answerMapper;
    @Override
    public PageInfo<Question> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Question> questions = questionMapper.selectForList(qo);
        for (Question question : questions) {
            int ansNum = answerMapper.selectForNum(question.getId());
            question.setAnsNum(ansNum);
        }
        return new PageInfo<>(questions);
    }

    @Override
    public void save(Question question) {
        questionMapper.insert(question);
    }

    @Override
    public Question selectById(Long id) {
        return questionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Answer> selectAnswersById(Long questionId) {
        return answerMapper.selectByQuestionsId(questionId);
    }
}
